
package cn.innosmart.aq.view.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.yuntongxun.kitsdk.ECDeviceKit;

import cn.innosmart.aq.R;
import cn.innosmart.aq.adapter.NearByPeopleAdapter;
import cn.innosmart.aq.bean.NearByPeople;
import cn.innosmart.aq.customize.CustomsizeDialog;
import cn.innosmart.aq.view.activity.FriendActivity;

/**
 * Created by David on 2015/9/15.
 */
public class NearByFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {
    private View view;
    private ListView lvNearBy;
    private RelativeLayout rlAll;
    private SwipeRefreshLayout mSwipeLayout;
    private NearByPeopleAdapter nearByPeopleAdapter;
    private FriendActivity activity;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = (FriendActivity) getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_nearby, container, false);
        mSwipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.id_swipe_ly);
        mSwipeLayout.setOnRefreshListener(this);
        mSwipeLayout.setColorScheme(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);
        lvNearBy = (ListView) view.findViewById(R.id.lv_nearby);
        nearByPeopleAdapter = new NearByPeopleAdapter(getActivity(), FriendActivity.nearByPeoples);
        lvNearBy.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                NearByPeople nearByPeople = FriendActivity.nearByPeoples.get(position);
                if (nearByPeople.isFriend()) {
                    ECDeviceKit.getIMKitManager().startConversationActivity(nearByPeople.getId(), nearByPeople.getNickName());
                } else {
                    String uid = nearByPeople.getId();
                    addDialog(nearByPeople);
                }
            }
        });
        lvNearBy.setAdapter(nearByPeopleAdapter);
        setHasOptionsMenu(true);
        setBar();
        return view;
    }

    private static final int REFRESH_COMPLETE = 0X110;
    private Toolbar toolbar;

    private void setBar() {
        toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
        toolbar.setOnMenuItemClickListener(onMenuItemClick);
    }

    private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            return true;
        }

    };

    @Override
    public void onRefresh() {
//        mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 2000);
        activity.updateLocation();
        lvNearBy.setEnabled(false);
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case REFRESH_COMPLETE:
                    mSwipeLayout.setRefreshing(false);
                    break;
            }
        }

        ;
    };

    public void notifyData() {

    }

    public void stopFresh() {
        lvNearBy.setEnabled(true);
        nearByPeopleAdapter.notifyDataSetChanged();
        mHandler.sendEmptyMessage(REFRESH_COMPLETE);
    }

    public void refresh() {
        if (nearByPeopleAdapter != null)
            nearByPeopleAdapter.notifyDataSetChanged();
    }


    private void addDialog(final NearByPeople nearByPeople) {
        final CustomsizeDialog addDialog = new CustomsizeDialog(getActivity());
        addDialog.setTitle(R.string.dialog_title_alert);
        addDialog.setContent("确认添加" + nearByPeople.getNickName() + "为好友吗？");
        addDialog.setPositiveButton(getString(R.string.sure), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.addFriend(nearByPeople.getId());
                addDialog.dismiss();
            }
        });
        addDialog.setNegativeButton(getString(R.string.cancel), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addDialog.dismiss();
            }
        });
        addDialog.setCancelable(false);
        addDialog.setCanceledOnTouchOutside(false);
        addDialog.show();
    }
}
